<!DOCTYPE html>
<html>
<!--
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
<title>Element Model Tests</title>
<script src="third_party/closure/closure/goog/base.js"></script>
<script src="platform/compat.js"></script>
<script src="platform/weak_map.js"></script>
<script src="path.js"></script>
<script src="model.js"></script>
<script src="element_model.js"></script>
<script src="test_common.js"></script>

<script>
goog.require('goog.testing.jsunit');
</script>
</head>
<body>

<div id="t">
  <span id="t2" modelscope="a.b"></span>
</div>

<script>

function testModel() {
  var d = document.createElement('div');
  d.model = 'hello world';
  assertEquals('hello world', d.model);
  assertEquals('hello world', d.computedModel);

  var d2 = d.appendChild(document.createElement('div'));
  assertUndefined(d2.model);
  assertEquals('hello world', d2.computedModel);
}

function testModelScope() {
  var d = document.createElement('div');
  d.model = {a: {b: 'hello world'}};
  assertObjectEquals({a: {b: 'hello world'}}, d.model);
  assertObjectEquals({a: {b: 'hello world'}}, d.computedModel);

  d.modelScope = 'a';
  assertObjectEquals({a: {b: 'hello world'}}, d.model);
  assertObjectEquals({b: 'hello world'}, d.computedModel);

  var d2 = d.appendChild(document.createElement('div'));
  assertEquals('', d2.modelScope);
  d2.modelScope = 'b';
  assertEquals('b', d2.modelScope);

  assertUndefined(d2.model);
  assertEquals('hello world', d2.computedModel);
}

function testModelScope2() {
  var d = document.createElement('div');
  d.model = {a: {b: 'hello world'}};
  assertObjectEquals({a: {b: 'hello world'}}, d.model);
  assertObjectEquals({a: {b: 'hello world'}}, d.computedModel);

  var d2 = d.appendChild(document.createElement('div'));
  assertEquals('', d2.modelScope);
  d2.modelScope = 'a';
  assertEquals('a', d2.modelScope);
}

function testObserveOnElement() {
  var d = document.createElement('div');

  var count = 0;
  function callback(c) {
    count++;
  }

  Model.observe(d, 'model', callback);

  d.model = {};
  Model.dirtyCheck();
  assertEquals(1, count);
}

function testObserveOnElement2() {
  var div = document.createElement('div');

  var count = 0;
  function callback(c) {
    count++;
  }

  var divModel = div;

  Model.observe(divModel, 'model', callback);

  divModel.model = {};
  Model.dirtyCheck();
  assertEquals(1, count);
}

function testModelCleared() {
  var parent = document.createElement('div');
  var child = parent.appendChild(document.createElement('div'));

  parent.model = 'a';
  child.model = 'b';
  Model.dirtyCheck();
  assertEquals('b', child.model);
  assertEquals('b', child.computedModel);

  var count = 0;

  Model.observe(child, 'model', function(val, oldVal) {
    count++;
    assertUndefined(val);
    assertEquals('b', oldVal);
  });

  child.model = undefined;
  Model.dirtyCheck();
  assertUndefined(child.model);
  assertEquals('a', child.computedModel);

  assertEquals(1, count);
}

function testMissingModelScope() {
  var div = document.createElement('div');
  assertEquals('', div.modelScope);
}

</script>
</body>
</html>
